package com.qczl.pme.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qczl.pme.dao.AreaDao;
import com.qczl.pme.dao.CameraDao;
import com.qczl.pme.dao.StbDao;
import com.qczl.pme.entity.PMECamera;
import com.qczl.pme.service.MettingRoomService;
import com.qczl.pme.vo.CameraVo;

/**
 * 会议室业务逻辑层实现类
 * 
 * @author SongR
 *
 */
@Service
public class MettingRoomServiceImpl implements MettingRoomService {

	@Autowired
	StbDao stbdao;

	@Autowired
	CameraDao cameradao;

	@Autowired
	AreaDao areadao;

	/**
	 * 获取县级会议室
	 */
	@Override
	public List<CameraVo> getCountyV1(String stbno, int start, int size) throws Exception {
		String sql = "select a.* from pme_basis_camera a,pme_basis_stb b,pme_basis_stb_camera c,pme_basis_area d where a.camera_id=c.camera_id and b.stb_id=c.stb_id and a.area_id=d.area_id and b.stb_no='"
				+ stbno + "' and a.camera_level=0 and a.camera_type=0 and d.area_level=0 order by a.camera_sort";
		List<PMECamera> cameras = cameradao.getBySqlL(sql, start, size);
		List<CameraVo> vos = new ArrayList<CameraVo>();
		for (PMECamera c : cameras) {
			CameraVo vo = new CameraVo();
			BeanUtils.copyProperties(c, vo);
			vo.setAreaid(c.getArea().getId());
			vo.setAreaname(c.getArea().getName());
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取县级总数
	 */
	@Override
	public Long countCountyV1(String stbno) throws Exception {
		String sql = "select count(1) from pme_basis_camera a,pme_basis_stb b,pme_basis_stb_camera c,pme_basis_area d where a.camera_id=c.camera_id and b.stb_id=c.stb_id and a.area_id=d.area_id and b.stb_no='"
				+ stbno + "' and a.camera_level=0 and a.camera_type=0 and d.area_level=0";
		return (long) cameradao.countBySql(sql).intValue();
	}

	/**
	 * 获取直属部门会场
	 * 
	 * @version v1
	 */
	@Override
	public List<CameraVo> getBranchV1(String stbno, int start, int size) throws Exception {
		String sql = "select a.* from pme_basis_camera a,pme_basis_stb b,pme_basis_stb_camera c where a.camera_id=c.camera_id and b.stb_id=c.stb_id and b.stb_no='"
				+ stbno + "' and a.camera_level=0 and a.camera_type=2 order by a.camera_sort";
		List<PMECamera> cameras = cameradao.getBySqlL(sql, start, size);
		List<CameraVo> vos = new ArrayList<CameraVo>();
		for (PMECamera c : cameras) {
			CameraVo vo = new CameraVo();
			BeanUtils.copyProperties(c, vo);
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 获取直属部门会场总数
	 * 
	 * @version v1
	 */
	@Override
	public Long countBranchV1(String stbno) throws Exception {
		String sql = "select count(1) from pme_basis_camera a,pme_basis_stb b,pme_basis_stb_camera c where a.camera_id=c.camera_id and b.stb_id=c.stb_id and b.stb_no='"
				+ stbno + "' and a.camera_level=0 and a.camera_type=2";
		return (long) cameradao.countBySql(sql).intValue();
	}

	/**
	 * 获取村级会场
	 * 
	 * @version v1
	 */
	@Override
	public List<CameraVo> getVillageV1(String stbno, String townid, int start, int size) throws Exception {
		String sql = "SELECT * from pme_basis_camera camera where camera.camera_id IN(select b.camera_id from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
				+ stbno + "' and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_pid = " + townid
				+ " UNION select  b.camera_id from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
				+ stbno + "'  and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_id = " + townid
				+ ") order by camera.camera_sort";
		List<PMECamera> cameras = cameradao.getBySqlL(sql, start, size);
		List<CameraVo> vos = new ArrayList<CameraVo>();
		for (PMECamera c : cameras) {
			CameraVo vo = new CameraVo();
			BeanUtils.copyProperties(c, vo);
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 获取村级会场数量
	 */
	@Override
	public Long countVillageV1(String stbno, String townid) throws Exception {
		String sql = "SELECT count(1) from pme_basis_camera camera where camera.camera_id IN(select b.camera_id from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
				+ stbno + "' and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_pid = " + townid
				+ " UNION select  b.camera_id from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
				+ stbno + "'  and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_id = " + townid
				+ ")";
		return (long) cameradao.countBySql(sql).intValue();
	}

}
